/**
 * Created by DELL on 2017/5/23.
 */
//主页控制器
var appIndex=angular.module('appTeacher',['ngResource','ngRoute','oc.lazyLoad','ngCookies']);
appIndex.controller("indexCtrl",function ($scope,$resource,$cookieStore) {
    //以下cg开头为页面中（最新，浏览量）等排序字段样式切换预制变量
    "use strict";
    $scope.cgcolor=10;
    $scope.cgcolor1=10;
    $scope.cgcolor2=10;
    $scope.cgcolor3=10;
    $scope.bg=0;
    $scope.i=0;
    $scope.j=0;
    //分页相关数据
    $scope.loginOut = function () {
        $cookieStore.remove('loginInfo');
        window.location.href = '../../../login.html';
    };
    $scope.pagenum=[];

    //创建获取年级数据发送数据模块，获取cookie
    var postdata={};
    try
    {
        //获取用户信息
        postdata.id=$cookieStore.get('loginInfo').empid;
        $scope.username=$cookieStore.get('loginInfo').name;
        var type=$cookieStore.get('loginInfo').usertype;
        if(type===2) { window.location.href="/Web/Login";}
    }
    catch (e){
        //获取用户信息失败则返回登录页面
        window.location.href="/Web/Login";
    }
    //暂停/播放视频
    $scope.pause=function(mediaid){
        var media=document.getElementById(mediaid);
        media.src = "../../../images/ss.mp4";
    };
    //初始化分页发送数据
    $scope.chapter_id='';
    $scope.knowledge_id='';
    $scope.material_id='';
    $scope.material_name='';
    $scope.exercises_place='';
    $scope.exercises_type='';
    $scope.exercises_difficlt='';
    $scope.pageindex=0;
    $scope.loadtype='';
    $scope.contentNum='';
    $scope.selectbg = '';

     $scope.postConttentData={
            chapter_id:$scope.chapter_id,
            material_id:$scope.material_id,
            knowledge_id:$scope.knowledge_id,
            exercises_place:$scope.exercises_place,
            exercises_type:$scope.exercises_type,
            exercises_difficlt:$scope.exercises_difficlt,
            pageindex:$scope.pageindex,
            loadtype:$scope.loadtype
        };

    //年级学科过滤
    $scope.cgIndex=function (n) {//选择选课时，获取数据
        $scope.i=n;
        var postChapterData={
            id:$scope.Allsubject[$scope.i].id,
            CourseName:$scope.Allsubject[$scope.i].courseName,
            parentid:0
        };
        httpServer.getChapter($resource,postChapterData,callService.getchapter);
    };
    //章节过滤
    $scope.chapterChange=function (parm) {

        $scope.postConttentData.chapter_id = parm;

        httpServer.getData($resource,$scope.postConttentData,callService.getdata);
    };
    //教材过滤
    $scope.materialChange=function (parm,name,num) {
        $scope.j=num;
        $scope.material_name=name;
        $scope.material_id=parm;

       
        var booktypedata={
            pubcourseid:$scope.Allsubject[$scope.i].id,
            bookname:$scope.material_name
        };
        httpServer.getBookType($resource,booktypedata,callService.getbooktype);
    };
    //册别过滤
    $scope.bootypeChange=function () {
        httpServer.getData($resource,$scope.postConttentData,callService.getdata);

    };
    //知识点过滤
    $scope.knowledgeChange = function (parm, num) {
        $scope.knowledge_id=parm;
        $scope.selectbg = num;
        $scope. postConttentData={
            chapter_id:$scope.chapter_id,
            material_id:$scope.material_id,
            knowledge_id:$scope.knowledge_id,
            exercises_place:$scope.exercises_place,
            exercises_type:$scope.exercises_type,
            exercises_difficlt:$scope.exercises_difficlt,
            pageindex:$scope.pageindex,
            loadtype:$scope.loadtype
        };
        httpServer.getData($resource,$scope.postConttentData,callService.getdata);
    };
    //场景过滤
    $scope.placeChange=function (parm,num) {
        $scope.exercises_place=parm;
        $scope.cgcolor1=num;
        $scope. postConttentData={
            chapter_id:$scope.chapter_id,
            material_id:$scope.material_id,
            knowledge_id:$scope.knowledge_id,
            exercises_place:$scope.exercises_place,
            exercises_type:$scope.exercises_type,
            exercises_difficlt:$scope.exercises_difficlt,
            pageindex:$scope.pageindex,
            loadtype:$scope.loadtype
        };
        httpServer.getData($resource,$scope.postConttentData,callService.getdata);
    };
    //题型过滤
    $scope.typeChange=function (parm,num) {
        $scope.exercises_type=parm;
        $scope.cgcolor2=num;
        $scope.postConttentData={
            chapter_id:$scope.chapter_id,
            material_id:$scope.material_id,
            knowledge_id:$scope.knowledge_id,
            exercises_place:$scope.exercises_place,
            exercises_type:$scope.exercises_type,
            exercises_difficlt:$scope.exercises_difficlt,
            pageindex:$scope.pageindex,
            loadtype:$scope.loadtype
        };
        httpServer.getData($resource,$scope.postConttentData,callService.getdata);
    };
    //难点过滤
    $scope.diffChange=function (parm,num) {
        $scope.exercises_difficlt=parm;
        $scope.cgcolor3=num;
        $scope. postConttentData={
            chapter_id:$scope.chapter_id,
            material_id:$scope.material_id,
            knowledge_id:$scope.knowledge_id,
            exercises_place:$scope.exercises_place,
            exercises_type:$scope.exercises_type,
            exercises_difficlt:$scope.exercises_difficlt,
            pageindex:$scope.pageindex,
            loadtype:$scope.loadtype
        };
        httpServer.getData($resource,$scope.postConttentData,callService.getdata);
    };
    //页面索引过滤
    $scope.pageindexChange=function (parm) {
        $scope.pageindex=parm;
        $scope. postConttentData={
            chapter_id:$scope.chapter_id,
            material_id:$scope.material_id,
            knowledge_id:$scope.knowledge_id,
            exercises_place:$scope.exercises_place,
            exercises_type:$scope.exercises_type,
            exercises_difficlt:$scope.exercises_difficlt,
            pageindex:$scope.pageindex,
            loadtype:$scope.loadtype
        };
        httpServer.getData($resource,$scope.postConttentData,callService.getdata);
    };
    //排序过滤
    $scope.loadtypeChange=function (parm) {
        $scope.loadtype=parm;
        $scope.bg=parm;
        $scope. postConttentData={
            chapter_id:$scope.chapter_id,
            material_id:$scope.material_id,
            knowledge_id:$scope.knowledge_id,
            exercises_place:$scope.exercises_place,
            exercises_type:$scope.exercises_type,
            exercises_difficlt:$scope.exercises_difficlt,
            pageindex:$scope.pageindex,
            loadtype:$scope.loadtype
        };
        httpServer.getData($resource,$scope.postConttentData,callService.getdata);
    };
    //上一页和下一页方法
    $scope.up=function(){
        $scope.pageindex+=1;
        $scope. postConttentData={
            chapter_id:$scope.chapter_id,
            material_id:$scope.material_id,
            knowledge_id:$scope.knowledge_id,
            exercises_place:$scope.exercises_place,
            exercises_type:$scope.exercises_type,
            exercises_difficlt:$scope.exercises_difficlt,
            pageindex:$scope.pageindex,
            loadtype:$scope.loadtype
        };
        httpServer.getData($resource,$scope.postConttentData,callService.getdata);
    };
    $scope.down=function () {
        $scope.pageindex-=1;
        $scope. postConttentData={
            chapter_id:$scope.chapter_id,
            material_id:$scope.material_id,
            knowledge_id:$scope.knowledge_id,
            exercises_place:$scope.exercises_place,
            exercises_type:$scope.exercises_type,
            exercises_difficlt:$scope.exercises_difficlt,
            pageindex:$scope.pageindex,
            loadtype:$scope.loadtype
        };
        httpServer.getData($resource,$scope.postConttentData,callService.getdata);
    };

    //创建后台调用数据接口方法
    var callService={
        getsubject:function (content) {//获取学科数据
            $scope.Allsubject=content.data;
            var postMaterialData={
                CourseName:$scope.Allsubject[$scope.i].courseName,
                id: $scope.Allsubject[$scope.i].id,
                parentid: 0
            };
            httpServer.getMaterial($resource,postMaterialData,callService.getmaterial);

        },
        getmaterial:function (content) {//获取教材数据
            $scope.Material=content.data;
            var booktypedata={
                pubcourseid:$scope.Allsubject[$scope.i].id,
                bookname:$scope.material_name
            };
           httpServer.getBookType($resource,booktypedata,callService.getbooktype);
        },
        //获取册别数据
        getbooktype:function (content) {
            $scope.booktype=content.data;
            var postChapterData={
                pubcourseid:$scope.Allsubject[$scope.i].id
            };
            httpServer.getChapter($resource,postChapterData,callService.getchapter);
        },
        getchapter:function (content) {//获取目录数据
            $scope.parentChapter=content.data;
            var postPointData={
                gradeid:$scope.Allsubject[$scope.i].id,
                CourseName: $scope.Allsubject[$scope.i].courseName,
                parentid:0
            };
            httpServer.getPoint($resource,postPointData,callService.getpoint);
        },
        getpoint:function(content){//获取知识点数据
            $scope.point=content.data;
            httpServer.getPlace($resource,callService.getplace);
        },
        getplace:function (content) {//获取场景数据
            $scope.place=content.data;
            httpServer.getType($resource,callService.gettype);
        },
        gettype:function (content) {//获取类型数据
            $scope.type=content.data;
            httpServer.getDiff($resource,callService.getdiff);
        },
        getdiff:function (content) {//获取难度数据
            $scope.diff=content.data;
            getdata();
            httpServer.getData($resource,$scope.postConttentData,callService.getdata);
        },
        getdata:function (content) {
         httpServer.getData($resource,$scope.postConttentData,function()
         {
             $scope.contentNum=content;
            $scope.contentData=content.data;
            var length=Math.ceil(content.dataCount/10);
            $scope.pagenum=[];
            for(var i=0;i<length;i++){
                $scope.pagenum.push([i]);
            }
         });
        }
    };

    //初始化数据
    httpServer.getSubject($resource,postdata,callService.getsubject);
});
